package com.prathiba.jumbledword;

import java.util.Stack;

/**
 * Contains the jumbled and the list of guesses entered by the user.
 * 
 */
public class GameUnit {

	private String jumbledWord;
	private Stack<Guess> guesses = new Stack<Guess>();

	public GameUnit(String jumbledWord) {
		this.jumbledWord = jumbledWord;
	}

	public String getJumbledWord() {
		return jumbledWord;
	}

	public void addGuess(Guess guess) {
		guesses.add(guess);
	}

	public Stack<Guess> getGuesses() {
		return guesses;
	}

	@Override
	public String toString() {
		return "WordGuess [jumbled_word=" + jumbledWord + ", guesses="
				+ guesses + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((guesses == null) ? 0 : guesses.hashCode());
		result = prime * result
				+ ((jumbledWord == null) ? 0 : jumbledWord.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		GameUnit other = (GameUnit) obj;
		if (guesses == null) {
			if (other.guesses != null)
				return false;
		} else if (!guesses.equals(other.guesses))
			return false;
		if (jumbledWord == null) {
			if (other.jumbledWord != null)
				return false;
		} else if (!jumbledWord.equals(other.jumbledWord))
			return false;
		return true;
	}

	/**
	 * If the valid dictionary word is entered by the user the score is
	 * incremented accordingly
	 * 
	 * @return int count
	 */
	public int getValidGuessCount() {
		int count = 0;
		for (Guess guess : guesses) {
			if (guess.getValid())
				count++;
		}
		return count;
	}

}
